const graphql = require("graphql");
const mysql = require("../db");

const OrderType = new graphql.GraphQLObjectType({
   name: "Order",
   fields: {
      id: {
         type: graphql.GraphQLInt,
      },
      orderNumber: {
         type: graphql.GraphQLInt,
      },
      status: {
         type: graphql.GraphQLString,
      },
      comments: {
         type: graphql.GraphQLString,
      },
   },
});

const userType = new graphql.GraphQLObjectType({
   name: "User",
   fields: {
      id: {
         type: graphql.GraphQLID,
      },
      username: {
         type: graphql.GraphQLString,
      },
      password: {
         type: graphql.GraphQLString,
      },
      nickname: {
         type: graphql.GraphQLString,
      },
      phone: {
         type: graphql.GraphQLString,
      },
      money: {
         type: graphql.GraphQLInt
      },
      token: {
         type: graphql.GraphQLString,
      },
      status: {
         type: graphql.GraphQLInt
      },
   },
});

const goodsType = new graphql.GraphQLObjectType({
   name: "Goods",
   fields: {
      id: {
         type: graphql.GraphQLID,
      },
      name: {
         type: graphql.GraphQLString,
      },
      detail: {
         type: graphql.GraphQLString,
      },
      cate: {
         type: graphql.GraphQLString,
      },
      num: {
         type: graphql.GraphQLInt,
      },
      rate: {
         type: graphql.GraphQLFloat,
      },
   },
});

const ResponseType = new graphql.GraphQLObjectType({
   name: "response",
   fields: {
      status: {
         type: graphql.GraphQLString
      },
      msg: {
         type: graphql.GraphQLString
      },
   },
});

const queryType = new graphql.GraphQLObjectType({
   name: "Query",
   fields: {
      orders: {
         type: new graphql.GraphQLList(OrderType),
         args: {
            page: {
               type: graphql.GraphQLInt,
            },
            pageSize: {
               type: graphql.GraphQLInt,
            },
         },
         resolve: async (_, {
               page = 0,
               pageSize = 10
            }) =>
            await mysql.doQuery(`
             SELECT  * FROM orders limit ${
               (page - 1 < 0 ? 0 : page - 1) * pageSize
             },${pageSize};
         `),
      },
      addOrders: {
         type: ResponseType,
         args: {
            orderNumber: {
               type: graphql.GraphQLInt,
            },
            comments: {
               type: graphql.GraphQLString,
            },
            status: {
               type: graphql.GraphQLString,
            },
         },
         async resolve(parenValue, args) {
            await mysql.doAdd(`
            INSERT INTO orders (status, orderNumber, comments) VALUES
             (${args.status},${args.orderNumber},${args.comments}) 
            `);
            return {
               statues: 0,
               msg: "ok"
            };
         },
      },
      login: {
         type: new graphql.GraphQLList(userType),
         args: {
            username: {
               type: graphql.GraphQLString,
            },
            password: {
               type: graphql.GraphQLString,
            },
         },
         resolve: async (parenValue, args) =>
            await mysql.doQuery(`
             SELECT  * FROM user where username = '${args.username}' and password = '${args.password}';
         `),
      },
      register: {
         type: ResponseType,
         args: {
            username: {
               type: graphql.GraphQLString,
            },
            nickname: {
               type: graphql.GraphQLString,
            },
            password: {
               type: graphql.GraphQLString,
            },
            phone: {
               type: graphql.GraphQLString,
            },
         },
         async resolve(parenValue, args) {
            let adduser = await mysql.doQuery(`
            INSERT INTO user (username, password, nickname, phone) VALUES
             ('${args.username}','${args.password}','${args.nickname}','${args.phone}') 
            `);
            console.log("adduser", adduser)
            return {
               statues: '0',
               msg: "ok"
            }
         },
      },
      goodsList: {
         type: new graphql.GraphQLList(goodsType),
         args: {
            page: {
               type: graphql.GraphQLInt,
            },
            pageSize: {
               type: graphql.GraphQLInt,
            },
         },
         resolve: async (_, {
               page = 0,
               pageSize = 10
            }) =>
            await mysql.doQuery(`
             SELECT  * FROM goods limit ${
               (page - 1 < 0 ? 0 : page - 1) * pageSize
             },${pageSize};
         `),
      },
      goodsDetail: {
         type: new graphql.GraphQLList(goodsType),
         args: {
            id: {
               type: graphql.GraphQLID,
            }
         },
         resolve: async (parenValue, args) =>
            await mysql.doQuery(`
             SELECT  * FROM goods where id = ${args.id};
         `),
      },
      goodsBuy: {
         type: ResponseType,
         args: {
            id: {
               type: graphql.GraphQLID,
            },
            userId: {
               type: graphql.GraphQLID,
            },
            quantity: {
               type: graphql.GraphQLInt,
            },
            amount: {
               type: graphql.GraphQLInt,
            },
         },
         async resolve(parenValue, args) {
            let user = await mysql.doQuery(`
            UPDATE user SET money=money-${args.amount} WHERE id = ${args.userId};
        `)
      //       let goods = mysql.doQuery(`
      //       UPDATE goods SET num=num-${args.quantity} WHERE id = ${args.id};
      //   `)

            console.info('user', user)
            // console.info('goods', goods)

            return {
               statues: '0',
               msg: "ok"
            };
         },
      }
   },
});

module.exports = {
   queryType,
};